package com.fqgj.sentry.controller.antiFraud.response.anti.tree.statistics;

import com.fqgj.common.utils.DateUtil;
import com.fqgj.sentry.controller.antiFraud.response.anti.OrderInfo;
import com.fqgj.sentry.controller.antiFraud.response.anti.tree.AntiRelationInfo;
import com.fqgj.sentry.controller.antiFraud.response.anti.tree.NodeTreeInfo;
import org.apache.commons.collections.CollectionUtils;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by liupengpeng on 2017/10/17.
 */
public class NodeStatisticsUtil implements Serializable {

    /**
     * * 画订单--属性--订单
     */
    public static NodeStatisticsVo getNodeOrderStatistic(AntiRelationInfo antiRelationInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        NodeStatisticsVo nodeStatisticsVo = new NodeStatisticsVo();
        NodeTreeInfo nodeTreeInfo = antiRelationInfo.getNodeTreeInfo();
        List<NodeTreeInfo> firstPropertyChildren = nodeTreeInfo.getChildren();

        Set<String> firstPropertyNum = new HashSet<>();
        Set<String> firstOrderNum = new HashSet<>();
        Map<String, OrderInfo> firstOrderMap = new HashMap<>();

        Set<String> secondPropertyNum = new HashSet<>();
        Set<String> secondOrderNum = new HashSet<>();
        Map<String, OrderInfo> secondOrderMap = new HashMap<>();

        if (CollectionUtils.isNotEmpty(firstPropertyChildren)) {

            for (NodeTreeInfo firstPropertyChild : firstPropertyChildren) {
                firstPropertyNum.add(firstPropertyChild.getNo());

                List<NodeTreeInfo> firstOrderChildren = firstPropertyChild.getChildren();
                if (CollectionUtils.isNotEmpty(firstOrderChildren)) {
                    //查询二级属性数  订单数
                    for (NodeTreeInfo firstOrderChild : firstOrderChildren) {
                        firstOrderNum.add(firstOrderChild.getNo());
                        firstOrderMap.put(firstOrderChild.getNo(), firstOrderChild.getOrderInfo());

                        List<NodeTreeInfo> secondPropertyChildren = firstOrderChild.getChildren();
                        if (CollectionUtils.isNotEmpty(secondPropertyChildren)) {
                            for (NodeTreeInfo secondPropertyChild : secondPropertyChildren) {
                                secondPropertyNum.add(secondPropertyChild.getNo());

                                List<NodeTreeInfo> secondOrderChildren = secondPropertyChild.getChildren();
                                if (CollectionUtils.isNotEmpty(secondOrderChildren)) {

                                    //查询三级属性数  订单数
                                    for (NodeTreeInfo secondOrderChild : secondOrderChildren) {
                                        secondOrderNum.add(secondOrderChild.getNo());
                                        secondOrderMap.put(firstOrderChild.getNo(), secondOrderChild.getOrderInfo());
                                    }

                                }
                            }
                        }
                    }
                }

            }

        }
        Integer firstOrderRejectNum = 0;
        Integer firstOrderPassNum = 0;
        Integer firstOrderOverdueNum = 0;
        if (firstOrderMap != null) {
            for (OrderInfo orderInfo : firstOrderMap.values()) {

                if (orderInfo.getRiskResultType() != null && orderInfo.getRiskResultType() == 1) {
                    firstOrderRejectNum++;
                }
                if (orderInfo.getRiskResultType() != null && orderInfo.getRiskResultType() == 3) {
                    firstOrderPassNum++;
                }
                if (orderInfo.getSuccessDate() != null && orderInfo.getRepaymentDate() != null) {
                    try {
                        if (orderInfo.getActualRepaymentDate() != null) {

                            if (DateUtil.diffDate(sdf.parse(orderInfo.getActualRepaymentDate()), sdf.parse(orderInfo.getRepaymentDate())) > 0) {
                                firstOrderOverdueNum++;
                            }
                        } else {
                            if (DateUtil.diffDate(new Date(), sdf.parse(orderInfo.getRepaymentDate())) > 0) {
                                firstOrderOverdueNum++;
                            }
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }

        }


        nodeStatisticsVo.setFirstNodeNum(firstPropertyNum.size())
                .setFirstOrderNum(firstOrderNum.size())
                .setFirstOrderRejectNum(firstOrderRejectNum)
                .setFirstOrderPassNum(firstOrderPassNum)
                .setFirstOrderOverdueNum(firstOrderOverdueNum);


        for (String s : firstPropertyNum) {
            if (secondPropertyNum.contains(s)) {
                secondPropertyNum.remove(s);
            }
        }
        for (String s : firstOrderNum) {
            if (secondOrderNum.contains(s)) {
                secondOrderNum.remove(s);
                secondOrderMap.remove(s);
            }
        }

        Integer secondOrderRejectNum = 0;
        Integer secondOrderPassNum = 0;
        Integer secondOrderOverdueNum = 0;
        if (firstOrderMap != null) {

            for (OrderInfo orderInfo : secondOrderMap.values()) {
                if (orderInfo.getRiskResultType() != null && orderInfo.getRiskResultType() == 1) {
                    firstOrderRejectNum++;
                }
                if (orderInfo.getRiskResultType() != null && orderInfo.getRiskResultType() == 3) {
                    firstOrderPassNum++;
                }
                if (orderInfo.getSuccessDate() != null && orderInfo.getRepaymentDate() != null) {
                    try {
                        if (orderInfo.getActualRepaymentDate() != null) {

                            if (DateUtil.diffDate(sdf.parse(orderInfo.getActualRepaymentDate()), sdf.parse(orderInfo.getRepaymentDate())) > 0) {
                                firstOrderOverdueNum++;
                            }
                        } else {
                            if (DateUtil.diffDate(new Date(), sdf.parse(orderInfo.getRepaymentDate())) > 0) {
                                firstOrderOverdueNum++;
                            }
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }
        }


        nodeStatisticsVo.setSecondNodeNum(secondPropertyNum.size())
                .setSecondOrderNum(secondOrderNum.size())
                .setSecondOrderRejectNum(secondOrderRejectNum)
                .setSecondOrderPassNum(secondOrderPassNum)
                .setSecondOrderOverdueNum(secondOrderOverdueNum);

        return nodeStatisticsVo;

    }


}
